/**
 * 
 */
package excel;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;

import po.MyObject;

/**
 * @author suwy
 * @date 2014-1-21
 * @todo TODO
 */
public class Exl4GanShu {

	private static String fileName = "2013年甘肃省医疗卫生机构基础大输液类药品集中采购商务标评审结果公示表";
	private static String filePath = "I:\\data\\CHD\\tender\\"+fileName+"\\";
	private static String outputFile = "i:/data/"+fileName+".xlsx";
	private static int startPage = 1;
	private static int rowNum = 35;
	private static String regex = "<tr >.*?<td style=\"text-align:center;\">(.*?)</td>.*?"
			+"<td style=\"text-align:center;\">(.*?)</td>.*?"
			+"<td style=\"text-align:center;\">(.*?)</td>.*?"
			+"<td style=\"text-align:center;\">(.*?)</td>.*?"
			+"<td style=\"text-align:center;\">(.*?)</td>.*?"
			+"<td style=\"text-align:center;\">(.*?)</td>.*?"
			+"<td style=\"text-align:center;\">(.*?)</td>.*?"
			+"<td style=\"text-align:center;\">(.*?)</td>.*?"
			+"<td style=\"text-align:center;\">(.*?)</td>.*?"
			+"<td style=\"text-align:center;\">(.*?)</td>.*?"
			+"<td style=\"text-align:center;\">(.*?)</td>.*?"
			+"<td style=\"text-align:center;\">(.*?)</td>.*?"
			+"<td style=\"text-align:center;\">(.*?)</td>.*?"
			+"<td style=\"text-align:center;\">(.*?)</td>.*?"
			+"<td style=\"text-align:center;\">(.*?)</td>.*?"
			+"<td style=\"text-align:center;\">(.*?)</td>.*?"
			+"<td style=\"text-align:center;\">(.*?)</td>.*?"
			+"<td style=\"text-align:center;\">(.*?)</td>.*?</tr>";

	static StringBuffer readHtm(String filePath){
		StringBuffer document = new StringBuffer();
		InputStreamReader fr = null;
		BufferedReader br = null;
		String line = null;
		try {
			fr = new InputStreamReader(new FileInputStream(filePath), "utf-8");
			br = new BufferedReader(fr);
			while ((line = br.readLine()) != null) {
				document.append(line);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (br != null) {
					br.close();
				}
				if (fr != null) {
					fr.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		System.out.println("NO."+filePath);
		return document;
	}
	
	static void writeExl(List<MyObject> list) {
		FileOutputStream fOut = null;
		try {
			Workbook workbook = new SXSSFWorkbook(2000);
			Sheet sheet = workbook.createSheet("sheet0");
			Row row = null;
			fOut = new FileOutputStream(outputFile);
				for (int i = 0; i < list.size(); i++) {
					row = sheet.createRow(i);
					row.createCell(0).setCellValue(list.get(i).getObj1().trim());
					row.createCell(1).setCellValue(list.get(i).getObj2().trim());
					row.createCell(2).setCellValue(list.get(i).getObj3().trim());
					row.createCell(3).setCellValue(list.get(i).getObj4().trim());
					row.createCell((short)4).setCellValue(list.get(i).getObj5().trim());
					row.createCell((short)5).setCellValue(list.get(i).getObj6().trim());
					row.createCell((short)6).setCellValue(list.get(i).getObj7().trim());
					row.createCell((short)7).setCellValue(list.get(i).getObj8().trim());
					row.createCell((short)8).setCellValue(list.get(i).getObj9().trim());
					row.createCell((short)9).setCellValue(list.get(i).getObj10().trim());
					row.createCell(10).setCellValue(list.get(i).getObj11().trim());
					row.createCell(11).setCellValue(list.get(i).getObj12().trim());
					row.createCell(12).setCellValue(list.get(i).getObj13().trim());
					row.createCell(13).setCellValue(list.get(i).getObj14().trim());
					row.createCell((short)14).setCellValue(list.get(i).getObj15().trim());
					row.createCell((short)15).setCellValue(list.get(i).getObj16().trim());
					row.createCell((short)16).setCellValue(list.get(i).getObj17().trim());
					row.createCell((short)17).setCellValue(list.get(i).getObj18().trim());
					System.out.println("第"+i+"行");
				}
			workbook.write(fOut);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (null != fOut) {
				try {
					fOut.close();
					System.out.println("success export excel");
				} catch (Exception e) {
					e.printStackTrace(System.err);
				}
			}
		}
	}
	
//	static List<MyObject> titleList(List<MyObject> list,int firstPage, StringBuffer text) {
//		
//		if(firstPage == startPage) {
//			Pattern pattern = Pattern.compile(titleRegex);
//			Matcher matcher = pattern.matcher(text);
//			MyObject o = new MyObject();
//			while (matcher.find()) {
//				o.setObj1(matcher.group(1).trim());
//				o.setObj2(matcher.group(2).trim());
//				o.setObj3(matcher.group(3).trim());
//				o.setObj4(matcher.group(4).trim());
//				o.setObj5(matcher.group(5).trim());
//				o.setObj6(matcher.group(6).trim());
//				o.setObj7(matcher.group(7).trim());
//				o.setObj8(matcher.group(8).trim());
//				o.setObj9(matcher.group(9).trim());
//				o.setObj10(matcher.group(10).trim());
//				o.setObj11(matcher.group(11).trim());
//				o.setObj12(matcher.group(12).trim());
//				o.setObj13(matcher.group(13).trim());
//				o.setObj14(matcher.group(14).trim());
//				o.setObj15(matcher.group(15).trim());
//				list.add(o);
//			}
//			
//		} else {
//			System.out.println("This is not first page , please check it !");
//		}
//		
//		return list;
//	}
	
	private void excute() {
		try {
			List<MyObject> list = new ArrayList<MyObject>();
			StringBuffer content = null;
			MyObject o = null;
			for (int i = startPage; i <= rowNum; i++) {
				content = readHtm(filePath+i+".htm");
				Pattern pattern = Pattern.compile(regex);
				Matcher matcher = pattern.matcher(content);
				while (matcher.find()) {
					o = new MyObject();
					o.setObj1(matcher.group(1).trim());
					o.setObj2(matcher.group(2).trim());
					o.setObj3(matcher.group(3).trim());
					o.setObj4(matcher.group(4).trim());
					o.setObj5(matcher.group(5).trim());
					o.setObj6(matcher.group(6).trim());
					o.setObj7(matcher.group(7).trim());
					o.setObj8(matcher.group(8).trim());
					o.setObj9(matcher.group(9).trim());
					o.setObj10(matcher.group(10).trim());
					o.setObj11(matcher.group(11).trim());
					o.setObj12(matcher.group(12).trim());
					o.setObj13(matcher.group(13).trim());
					o.setObj14(matcher.group(14).trim());
					o.setObj15(matcher.group(15).trim());
					o.setObj16(matcher.group(16).trim());
					o.setObj17(matcher.group(17).trim());
					o.setObj18(matcher.group(18).trim());
					
					list.add(o);
				}
			}
			writeExl(list);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		Exl4GanShu instance = new Exl4GanShu();
		instance.excute();
	}

}
